<?php
namespace app\system\controller;

use app\common\model\DeptModel;
use think\Cache;
use think\Request;

class DeptController extends BaseController
{

    private $deptModel;
    private $deptCache;
    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->deptModel = new DeptModel();
        $this->deptCache = Cache::get('dept');
    }

    /**
     * 栏目首页
     */
    public function index()
    {
        $this->assign('dept', $this->deptCache);
        return $this->fetch();
    }

    /**
     * 栏目的添加
     * @return mixed
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            try{
                $data = $this->request->param();
                $result = $this->validate($data, [
                    'dept_name|部门名称' => 'require',
                    'dept_code|部门编码' => 'require'
                ]);
                if ($result !== true) {
                    exception($result,40001);
                }
                $this->deptModel->addData($data);
                $this->deptModel->updateCache();
            } catch (\Exception $e){
                $this->error($e->getMessage());
            }
            $this->success('添加成功', url('index'));
        }
        $this->assign('dept', $this->deptCache);
        return $this->fetch();
    }

    /**
     * 修改栏目
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            try{
                $data   = $this->request->param();
                $result = $this->validate($data, [
                    'dept_name|部门名称' => 'require',
                    'dept_code|部门编码' => 'require'
                ]);
                if ($result !== true) {
                    exception($result,40001);
                }
                $this->deptModel->editData($data);
                $this->deptModel->updateCache();
            } catch (\Exception $e){
                $this->error($e->getMessage());
            }
            $this->success('操作成功！', url('index'));
        }
        $deptId   = $this->request->param('dept_id/d', 0);
        $info     = $this->deptCache[$deptId];
        $dept = [];
        foreach ($this->deptCache as $k => $v) {
            //父栏目select状态
            $v['selected'] = $info['dept_fid'] == $v['dept_id'] ? 'selected' : '';
            //子栏目disabled
            $disabled      = \org\util\TreeUtil::isChild($this->deptCache, $v['$dept_id'], $deptId, 'dept_fid') || $deptId == $v['dept_id'] ? 'disabled' : '';
            $v['disabled'] = $disabled;
            $dept[]    = $v;
        }
        $this->assign('dept', $dept);
        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 删除栏目
     */
    public function del()
    {
        if ($this->request->isAjax()) {
            try{
                $deptId = $this->request->param('dept_id/d');
                if ($this->deptModel->where('dept_fid', $deptId)->find()) {
                    exception('该栏目还存在下级部门，不允许删除！',40001);
                } else {
                    $this->deptModel->delData($deptId);
                    $this->deptModel->updateCache();
                }
            } catch (\Exception $e){
                $this->error($e->getMessage());
            }
            $this->success('删除成功！', url('index'));
        }
    }

    /**
     * 更新站点缓存
     */
    public function updateCache()
    {
        $this->deptModel->updateCache();
        $this->success('操作成功！', url('index'));
    }

    /**
     *栏目排序
     */
    public function updateOrder()
    {
        try{
            $listOrder = $this->request->param('list_order/a');
            foreach ($listOrder as $dept_id => $order) {
                $deptId        = intval($dept_id);
                $deptSort      = intval($order);
                $dataList[] = ["dept_id" => $deptId, "dept_sort" => $deptSort];
            }
            $this->deptModel->saveAll($dataList);
            $this->deptModel->updateCache();
        } catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success('排序成功！', url('index'));
    }

}
